package com.keeson.android_cli.protocol;


import com.keeson.android_cli.util.Crc32;
import com.keeson.android_cli.util.DataConverter;
import com.keeson.android_cli.util.HexDump;

/**
 * Created by zhoujiaqi on 2019/11/6.
 */
public class TSICMDTPFactory {

    private volatile static TSICMDTPFactory client ;
    public TSICMDTPFactory() {
        System.out.println(" created " + getClass().getSimpleName());
    }
    public static TSICMDTPFactory getInstance() {
        if (client == null)
            synchronized (TSICMDTPFactory.class) {
                if (client == null) {
                    client = new TSICMDTPFactory();
                }
            }
        return client;
    }


    /**
     * 发送数据包
     * @param
     * @param key
     * @return
     */
    public  byte[]  sendPackageLevelAndValue(int level,int levelvalue,int side,byte[] key){
        byte[] data = new byte[15];
        data[0] = (byte) 0xAC;
        data[1] = 0x02;
        data[2] = 0x00;
        data[3] = 0x09;
        data[4] = 0x01;

        System.arraycopy(key, 0, data, 5, 4);

        data[9] = (byte) level;

        byte[] levelvalues = new byte[2];
        levelvalues = DataConverter.getInstance().fromShort((short) levelvalue);
        //LogUtils.e("levelvalues======",HexDump.toHexString(levelvalues)+"");

        System.arraycopy(HexDump.reverseArray(levelvalues), 0,data , 10, 2);

        data[12] = (byte) side;
        byte[] crcCheckData =  new byte[13];
        System.arraycopy(data, 0, crcCheckData, 0, 13);
        data[13] = Crc32.getCRC32(crcCheckData)[0];
        data[14] = 0x55;
        return data;
    }



    /**
     * 发送数据包
     * @param
     * @param key
     * @return
     */
    public  byte[]  sendPackage(int param,int side,byte[] key){
        byte[] data = new byte[13];
        data[0] = (byte) 0xAC;
        data[1] = 0x02;
        data[2] = 0x00;
        data[3] = 0x07;
        data[4] = 0x01;
        System.arraycopy(key, 0, data, 5, 4);
        data[9] = (byte) param;
        data[10] = (byte) side;
        byte[] crcCheckData =  new byte[11];
        System.arraycopy(data, 0, crcCheckData, 0, 11);
        data[11] = Crc32.getCRC32(crcCheckData)[0];
        data[12] = 0x55;
        return data;
    }




    /**
     * 发送数设置评率据包
     * @param
     * @param key
     * @return
     */
    public  byte[]  sendPackageData2(int subcmd,int freq,byte[] key){
        byte[] data = new byte[9];
        data[0] = (byte) (9);
        data[1] = 0;//   数据帧 序号
        data[2] = 0x04;
        data[3] =(byte) subcmd;
        System.arraycopy(key, 0, data, 4, 4);
        data[8] = (byte) freq;
        return data;
    }













}
